/**
 * ATuanZhang
 */
package com.atuanzhang.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.huanglei.android.inject.annotation.InjectService;
import org.huanglei.android.util.AndroidHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.atuanzhang.Constant;
import com.atuanzhang.bean.City;
import com.atuanzhang.cache.Cache;
import com.atuanzhang.service.CityService;
import com.google.inject.Inject;
import com.google.inject.Provider;

/**
 * @author huanglei
 * @date 2011-3-25 @
 */
@InjectService
public class CityServiceImpl implements CityService {

	private final static String TAG = "CityService";

	private final static String KEY_PINYIN = "city_pinyin";
	private final static String KEY_NAME = "city";

	private final static String KEY_HOTCITYS = "hotCitys";
	private final static String KEY_CITYS = "citys";

	@Inject
	private Cache cache = null;

	@Inject
	protected Provider<AndroidHttpClient> httpClientProvider = null;

	/**
	 * 根据API获取城市数据
	 * @return
	 */
	private Map<String, List<City>> findCitysFromRemote() {
		Map<String, List<City>> cityMap = new HashMap<String, List<City>>();
		try {
			HttpResponse response = httpClientProvider.get().httpGet(
					Constant.REMOTE_URL_CITY);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

				JSONObject jsonObject = new JSONObject(EntityUtils.toString(
						response.getEntity(), HTTP.UTF_8));

				/**
				 * 热门城市
				 */
				JSONArray cityArrJson = jsonObject.getJSONArray(KEY_HOTCITYS);
				List<City> hotCityList = new ArrayList<City>();
				for (int i = 0; i < cityArrJson.length(); ++i) {
					hotCityList.add(new City("", cityArrJson.getString(i)));
				}
				cityMap.put(KEY_HOTCITYS, hotCityList);

				/**
				 * 城市
				 */
				cityArrJson = jsonObject.getJSONArray(KEY_CITYS);
				List<City> cityList = new ArrayList<City>();
				JSONObject city = null;
				for (int i = 0; i < cityArrJson.length(); i++) {
					city = cityArrJson.getJSONObject(i);
					cityList.add(new City(city.getString(KEY_PINYIN), city
							.getString(KEY_NAME)));
				}
				cityMap.put(KEY_CITYS, cityList);

			}
		} catch (IOException e) {
			Log.e(TAG, "findCitysFromRemote IOException", e);
		} catch (JSONException e) {
			Log.e(TAG, "findCitysFromRemote JSONException", e);
		}

		return cityMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.atuanzhang.service.CityService#findHotCitys()
	 */
	@Override
	public List<City> findHotCitys() {
		return findCitysFromRemote().get(KEY_HOTCITYS);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.atuanzhang.service.CityService#findCitys()
	 */
	@Override
	public List<City> findCitys() {
		return findCitysFromRemote().get(KEY_CITYS);
	}

}
